from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework import status
from .models import User
from .serializers import UserModelSerializer
from . import service
# 调用日志记录对象
import logging
log = logging.getLogger("django")


class CheckMobileAPIView(APIView):
    """
    验证手机号是否注册
    """
    def get(self,request,mobile):
        try:
            User.objects.get(mobile=mobile)
            result = False
            message= "手机号码已注册！"
            code = status.HTTP_400_BAD_REQUEST
        except User.DoesNotExist:
            result = True
            message = ""
            code = status.HTTP_200_OK
        return Response({"result":result,"message":message},status=code)



class GetTelAPIView(APIView):
    # permission_classes = [IsAuthenticated]    # 权限控制，必须登录的用户才能访问
    def get(self, request):
        try:
            queryset = User.objects.all()
            for user_obj in queryset:
                if user_obj.username != "admin":
                    tel = user_obj.tel
                    return Response({"result": "success", "tel": tel})
                else:
                    return Response({"result": "failure", "error_code": "10001",
                                     "error_reason": "get tel error"})

        except Exception as error:
            log.error("fun:get_tel:{}".format(error))
            return Response({"result": "failure", "error_code": "10002",
                             "error_reason": "get tel system error"})


class UserUpdateAPIView(ViewSet):
    # permission_classes = [IsAuthenticated]    # 权限控制，必须登录的用户才能访问
    def update(self, request):
        try:
            username = request.data.get("username")
            password = request.data.get("password")

            if not all([username, password]):
                return Response({"result": "failure", "error_code": "10003",
                                 "error_reason": "update_user_info_error"})

            user = User.objects.get(username=username)
            if not user:
                return Response({"result": "failure", "error_code": "10004",
                                 "error_reason": "update_user_error"})
            if len(password) < 6 or len(password) > 20:
                return Response({"result": "failure", "error_code": "10005",
                                 "error_reason": "update_user_len_error"})

            # user.enable_password = service.encryption(password)
            user.set_password(password)
            user.save()
            return Response({"result": "success", "user_status": "password update successfully"})
        except Exception as error:
            log.error("fun:update_user_password:{}".format(error))
            return Response({"result": "failure", "error_code": "10006",
                             "error_reason": "update_user_info_sys_error"})


class UserAPIView(CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserModelSerializer












